//
//  XZIpTool.m
//  生活小助手
//
//  Created by lixiangzhou on 14-8-7.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "XZIpTool.h"
#import "XZHttpTool.h"
#import "MJExtension.h"
#import "XZIpResult.h"
/*
 200101	ip地址不能为空
 200102	错误的IP地址
 200103	查询无结果
 200104	要查询的地址不能为空
 200105	查询无结果
 */

@implementation XZIpTool
+ (void)getIpWithParameter:(id)parameter success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    NSDictionary *dictParam = [parameter keyValues];
    
    [XZHttpTool getWithUrl:@"http://apis.juhe.cn/ip/ip2addr" parameters:dictParam success:^(NSDictionary *responseObj) {
        int errorcode = [responseObj[@"error_code"] intValue];
        if (errorcode) {
            NSString *domain;
            if (errorcode == 200101) {
                domain = @"ip地址不能为空";
            } else if (errorcode == 200102) {
                domain = @"错误的IP地址";
            } else if (errorcode == 200103) {
                domain = @"查询无结果";
            } else if (errorcode == 200104) {
                domain = @"要查询的地址不能为空";
            } else if (errorcode == 200105) {
                domain = @"查询无结果";
            }
            
            NSError *ipError = [NSError errorWithDomain:domain code:errorcode userInfo:nil];
            if (failure) {
                failure(ipError);
            }
        } else {
            
            id resultObj = [[XZIpResult class] objectWithKeyValues:responseObj];
            if (success) {
                success(resultObj);
            }
        }
    } failure:^(NSError *error) {
        NSError *ipError = [NSError errorWithDomain:@"网络无连接" code:[error code] userInfo:error.userInfo];
        if (failure) {
            failure(ipError);
        }
    }];
}
@end
